package com.nnxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nnxy.mapper.ClazzMapper;
import com.nnxy.pojo.Clazz;
import com.nnxy.pojo.Emp;
import com.nnxy.pojo.PageResult;
import com.nnxy.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    public PageResult<Clazz> findByPage(Integer page, Integer pageSize, String name, LocalDate begin, LocalDate end) {
        PageHelper.startPage(page,pageSize);

        Page<Clazz> pageObj = clazzMapper.findByPage(name, begin, end);
        long total = pageObj.getTotal();
        List<Clazz> rows = pageObj.getResult();

        return new PageResult(rows,total);
    }


    @Override
    public void add(Clazz clazz) {
        //1.补全班级数据
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());

        //2.调用mapper插入班级数据
        clazzMapper.insert(clazz);
    }

    public List<Emp> list() {
        return clazzMapper.findAll();
    }

    @Override
    public Clazz findById(Integer id) {
        return clazzMapper.findById(id);
    }

    @Override
    public void modify(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public void removeById(Integer id) {
        clazzMapper.removeById(id);
    }
    @Override
    public boolean hasEmployees(Integer clazzId) {
        int count = clazzMapper.hasEmployees(clazzId);
        return count > 0;
    }

    @Override
    public List<Clazz> queryAllClazz() {
        return clazzMapper.queryAllClazz();
    }
}
